package com.itboye.sunsun.mycollect.fragments;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.RelativeLayout;

import com.itboye.sunsun.R;
import com.itboye.sunsun.adapter.CollectGoodsAdapter;
import com.itboye.sunsun.adapter.CollectWordsAdapter;
import com.itboye.sunsun.adapter.MyCollectAdapter;
import com.itboye.sunsun.application.App;
import com.itboye.sunsun.beans.CollectWordsBean;
import com.itboye.sunsun.beans.CollectWordsBean.CollectWordsInnerBean;
import com.itboye.sunsun.beans.MyCollectBean.CollectGoodsBean;
import com.itboye.sunsun.beans.MyCollectBean;
import com.itboye.sunsun.constants.SPContants;
import com.itboye.sunsun.custome.XListView;
import com.itboye.sunsun.luntan.TieZiDetailActivity;
import com.itboye.sunsun.network.HttpRequest;
import com.itboye.sunsun.person.ui.MyCollectActivity.DeleteGoodsListener;
import com.itboye.sunsun.person.ui.MyCollectActivity.DeleteWordsListener;
import com.itboye.sunsun.support.BaseFragment;
import com.itboye.sunsun.utils.DebugLog;
import com.itboye.sunsun.utils.DebugToast;
import com.itboye.sunsun.utils.SPUtils;
import com.itboye.sunsun.volley.CodeErrorException;
import com.itboye.sunsun.volley.MyJsonRequest;
import com.itboye.sunsun.volley.XErrorListener;
import com.itboye.sunsun.volley.XRequestListener;
@SuppressLint("ValidFragment")
public class CollectWordsFragment extends BaseFragment implements OnItemClickListener ,DeleteWordsListener{

	private Context context;
	private XListView xlistview;
	private CollectWordsAdapter mAdapter;
	private List<CollectWordsInnerBean> list;
	private View v;
	private List<String> favoriteList;
	private RelativeLayout gonerela;
	private String favourStr="";

	public CollectWordsFragment(Context context) {
		this.context = context;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		v = inflater.inflate(R.layout.fragment_mycollectwords, null);

		getCollectWords();
		// initData();
		xlistview=(XListView) v.findViewById(R.id.xlistview);
		xlistview.setPullLoadEnable(false);
		xlistview.setPullRefreshEnable(false);
		xlistview.setOnItemClickListener(this);
		return v;
	}


	@Override
	public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {

         Intent intent=new Intent(App.ctx,TieZiDetailActivity.class);
         intent.putExtra("fid", list.get(position-1).getFid());
         intent.putExtra("tid", list.get(position-1).getTid());
         intent.putExtra("author_id", list.get(position-1).getAuthorId());
         startActivity(intent);
		
		
	}

	public void getCollectWords() {

		MyJsonRequest<CollectWordsBean> request = new MyJsonRequest.Builder<CollectWordsBean>().apiVer("100")
				.typeKey("BY_ForumThread_getFavorites")
				.param("uid", (String) SPUtils.get(App.ctx, null, SPContants.USER_ID, ""))
				.param("page_no", "1")
				.param("page_size", "100").requestListener(new XRequestListener<CollectWordsBean>() {

					@Override
					public void onResponse(CollectWordsBean response) {
						DebugLog.v("result", response.toString());

						//DebugToast.show(response.getList().get(0).getFavoriteId(), 0);
						int count=Integer.parseInt(response.getCount());
						if(count>0)
						{
						
						   list = response.getList();
						   mAdapter = new CollectWordsAdapter(App.ctx, list);
						   xlistview.setAdapter(mAdapter);
						   gonerela.setVisibility(View.GONE);
						}else
						{
							list=new ArrayList<>();
							gonerela.setVisibility(View.VISIBLE);
						}

					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code, String msg) {
						if (exception instanceof CodeErrorException) {
							Toast.makeText(App.ctx, "网络异常", 0).show();
						} else {
							Toast.makeText(App.ctx, "网络异常", 0).show();
						}

					}
				}).build();
		HttpRequest.getDefaultRequestQueue().add(request);

	}

	@Override
	public void onDeleteCollectWords() {
		favoriteList = new ArrayList<>();
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).isSelected()) {

				favoriteList.add(list.get(i).getTid());
				
				//如果大于0就执行
				if(list.size()>0)
				{
					
					//如果只有一个
					if(list.size()==1)
					{
						favourStr=favourStr+list.get(i).getFavoriteId();
					}
					//如果有多个
					else if(list.size()>1)
					{
						
					  favourStr=favourStr+list.get(i).getFavoriteId()+",";
					  if(i==list.size()-1)
					  {
						favourStr=favourStr+list.get(i).getFavoriteId();
					  }
					}
				}


			}
		}
	//	Toast.makeText(App.ctx, "" + list.get(0).getFavouriteId(), 0).show();
		if(favoriteList.size()>0)
		{
	     	deleteCollectWords(favourStr);
		}
		
	}

	private void deleteCollectWords(String favoriteStr) {
		MyJsonRequest<String> request = new MyJsonRequest.Builder<String>()
				.apiVer("100")
				.typeKey("BY_Favorites_cancel")
				.param("type", 2)
				.param("favorite_id", favoriteStr).requestListener(new XRequestListener<String>() {

					@Override
					public void onResponse(String response) {
						DebugLog.v("result", response.toString());
						// list.remove(location)

						Iterator<CollectWordsInnerBean> itor = list.iterator();
						while (itor.hasNext()) {
							CollectWordsInnerBean bean = itor.next();
							if (bean.isSelected()) {
								itor.remove();
							}
						}

						mAdapter.notifyDataSetChanged();
						// Toast.makeText(App.ctx, "" + response, 0).show();

					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code, String msg) {
						if (exception instanceof CodeErrorException) {
							Toast.makeText(App.ctx,"网络异常", 0).show();
						} else {
							Toast.makeText(App.ctx, "网络异常", 0).show();
						}

					}
				}).build();
		HttpRequest.getDefaultRequestQueue().add(request);
		
	}

}
